package com.acme.bankapp;

import com.acme.bankapp.domain.Bank;
import com.acme.bankapp.domain.Client;
import com.acme.bankapp.domain.Gender;
import com.acme.bankapp.domain.account.CheckingAccount;
import com.acme.bankapp.domain.account.SavingAccount;
import com.acme.bankapp.exception.BankException;
import com.acme.bankapp.exception.ClientExistsException;
import com.acme.bankapp.exception.IllegalAccountCreationException;
import com.acme.bankapp.service.BankService;

import java.io.IOException;

public class BankApplication {
    private static boolean shouldBeSavedAsFeed = false;
    private static String destinationFile = "bank.ser";

    public static void main(String[] args) throws IOException {
        Bank bank = null;
        try {
            if (args.length == 0) {
                bank = new Bank();
                fillBankWithDemoData(bank);
            } else {
                bank = initiateBank(args);
            }
        } catch (BankException e) {
            System.err.println("Cannot load bank from file. Inconsistent data.");
            return;
        }

        BankService.printBalance(bank);

        if (shouldBeSavedAsFeed) {
            BankDataLoaderService.saveBank(bank, destinationFile);
        } else {
            BankService.saveBank(bank, destinationFile);
        }
    }

    private static Bank initiateBank(String[] args) throws BankException, IOException {
        Bank bank = null;
        for (int i = 0; i < args.length; i++) {
            switch (args[i]) {
                case "-loadfeed":
                    destinationFile = args[++i];
                    bank = BankDataLoaderService.loadBank(destinationFile);
                    shouldBeSavedAsFeed = true;
                    break;
                case "-loadbank":
                    destinationFile = args[++i];
                    bank = BankService.loadBank(destinationFile);
                    break;
                case "-server":
                    if (bank == null) {
                        bank = new Bank();
                        fillBankWithDemoData(bank);
                    }
                    new BankServer(bank).newConnection(888);
                    break;
                default:
                    System.err.println("Unknown key: " + args[i]);
            }
        }
        return bank;
    }

    private static void fillBankWithDemoData(Bank bank) throws BankException {
        Client client1 = new Client("Alex Dobrov", Gender.MALE);
        Client client2 = new Client("Anya Fedorova", Gender.FEMALE);
        try {
            client1.addAccount(new SavingAccount(15.5));
            client2.addAccount(new CheckingAccount(10.5, 20.));
            BankService.addClient(bank, client1);
            BankService.addClient(bank, client2);
        } catch (IllegalAccountCreationException e) {
            System.out.println("Initial deposit and/or Withdraw is less then zero");
        } catch (ClientExistsException e) {
            System.out.println("Client with name " + e.getMessage() + " already exists");
        }
    }
}